package com.crm.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.domain.dto.ActivityChannelStatType;

import com.crm.domain.entity.ActivityChannelStat;
import com.crm.mapper.ActivityChannelStatMapper;
import com.crm.service.ActivityChannelStatService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class ActivityChannelStatServiceImpl extends ServiceImpl<ActivityChannelStatMapper, ActivityChannelStat> implements ActivityChannelStatService {
    @Override
    public Page<ActivityChannelStat> getByPage(ActivityChannelStatType activityChannelStatType) {
        //1.设置分页参数
        /*
            请求list和请求分页都是同一个接口 如果不对空值进行初始化 会有空指针异常。
            解决方式:手动设置初始值 但ObjectUtil.isEmpty会把null也判断为对象 需要取值或者queryDictType==null
         */
        Page<ActivityChannelStat> page = ObjectUtil.isEmpty(activityChannelStatType.getPageNum())?Page.of(1, 10):Page.of(activityChannelStatType.getPageNum(), activityChannelStatType.getPageSize());

        Map<String, Object> params = activityChannelStatType.getParams();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        LocalDate beginCreateTime =ObjectUtil.isEmpty(params.get("beginCreateTime"))?null:(LocalDate.parse((String)params.get("beginCreateTime"), formatter));
        LocalDate endCreateTime =ObjectUtil.isEmpty(params.get("endCreateTime"))?null:(LocalDate.parse((String)params.get("endCreateTime"), formatter));
        LocalDate beginTime =ObjectUtil.isEmpty(params.get("beginTime"))?null:(LocalDate.parse((String)params.get("beginTime"), formatter));
        LocalDate endTime =ObjectUtil.isEmpty(params.get("endTime"))?null:(LocalDate.parse((String)params.get("endTime"), formatter));


        //2.调用Mapper查询数据
        //2.1构建查询条件
        LambdaQueryWrapper<ActivityChannelStat> wrapper = Wrappers.<ActivityChannelStat>lambdaQuery()
                .like(ObjectUtil.isNotEmpty(activityChannelStatType.getCode()),ActivityChannelStat::getCode, activityChannelStatType.getCode())
                .eq(ObjectUtil.isNotEmpty(activityChannelStatType.getInfo()),ActivityChannelStat::getInfo, activityChannelStatType.getInfo())
                .eq(ObjectUtil.isNotEmpty(activityChannelStatType.getName()),ActivityChannelStat::getName, activityChannelStatType.getName())
                .eq(ObjectUtil.isNotEmpty(activityChannelStatType.getTotal()),ActivityChannelStat::getTotal, activityChannelStatType.getTotal())
                .between(ObjectUtil.isNotEmpty(beginCreateTime)&&ObjectUtil.isNotEmpty(endCreateTime)
                        ,ActivityChannelStat::getCreateTime, beginCreateTime, endCreateTime)
                .between(ObjectUtil.isNotEmpty(beginTime)&&ObjectUtil.isNotEmpty(endTime)
                        ,ActivityChannelStat::getCreateTime, beginTime, endTime);

        //2.2用m-plus查询
        Page<ActivityChannelStat> dictTypePage = page(page, wrapper);
        //        Page<ActivityChannelStat> dictTypePage = page(page, wrapper);

        return dictTypePage;
    }
}
